package com.eco.fanliapp.admin.controller.web;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eco.fanliapp.admin.common.BaseController;
import com.eco.fanliapp.common.response.ServerResponse;
import com.eco.fanliapp.entity.SySendSmTemplate;
import com.eco.fanliapp.service.ISySendSmTemplateService;
import com.eco.fanliapp.utils.StringUtils;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 短信模板表 前端控制器
 * </p>
 *
 * @author jobob
 * @since 2019-12-25
 */
@RestController
@RequestMapping("/web/smTemplate")
public class SySendSmTemplateController extends BaseController {

	@Autowired
	private ISySendSmTemplateService sySendSmTemplateService;

	/**
	 * 短信模板列表
	 * @param
	 * @return
	 */
	@GetMapping("/list")
	@ResponseBody
	public ServerResponse listTemplate(){

		QueryWrapper<SySendSmTemplate> query = new QueryWrapper<>();
		query.eq("enable",1);
		query.eq("deleted",0);
		List<SySendSmTemplate> list = sySendSmTemplateService.list(query);
		if(CollectionUtils.isNotEmpty(list)){
			return ServerResponse.createBySuccess("消息模板列表",list);
		}
		return ServerResponse.createByErrorMessage("短信模板列表为空");
	}
	/**
	 * 添加短信模板
	 * @param sendSmTemplate
	 * @return
	 */
	@PostMapping("/add")
	public ServerResponse addTemplate(@RequestBody SySendSmTemplate sendSmTemplate){
		//校验
		String smTemplateName = sendSmTemplate.getSmTemplateName();
		if(StringUtils.isNull(smTemplateName)){
			return ServerResponse.createByErrorMessage("模板名称不能为空");
		}
		String templateContent = sendSmTemplate.getTemplateContent();
		if(StringUtils.isNull(templateContent)){
			return ServerResponse.createByErrorMessage("模板内容不能为空");
		}
		sySendSmTemplateService.save(sendSmTemplate);
		return ServerResponse.createBySuccessMessage("模板创建成功");
	}

	/**
	 * 更新模板/删除模板
	 * @param sendSmTemplate
	 * @return
	 */
	@PostMapping("/update")
	public ServerResponse updateTemplate(@RequestBody SySendSmTemplate sendSmTemplate){
		//校验
		SySendSmTemplate template = sySendSmTemplateService.getById(sendSmTemplate.getTableId());
		if(template == null){
			return ServerResponse.createByErrorMessage("模板id非法!");
		}
		sySendSmTemplateService.updateById(sendSmTemplate);
		return ServerResponse.createBySuccessMessage("操作成功");
	}
}
